package com.bsoft.gol.hcs.clinical.dao;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.converter.ConversionUtils;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;
import pcn.prescribe.DisposalExpenseRecord;

import java.util.List;
import java.util.Map;

public abstract class DisposalExpenseRecordDAO extends HibernateSupportDelegateDAO<DisposalExpenseRecord> {

    @DAOMethod(sql = " from DisposalExpenseRecord where consultId =:consultId ")
    public abstract List<DisposalExpenseRecord> queryDisposalExpenseRecord(@DAOParam("consultId") String consultId);

    @DAOMethod(sql = " from DisposalExpenseRecord where itemId =:itemId ")
    public abstract DisposalExpenseRecord getDisposalExpenseRecordByItemId(@DAOParam("itemId") String itemId);

    @DAOMethod(sql = "update DisposalExpenseRecord set state=:state,applicationNo=:applicationNo where  itemId =:itemId ")
    public abstract void updateStateAndApplicationNoByItemId( @DAOParam("state") Byte state,@DAOParam("applicationNo") String applicationNo,@DAOParam("itemId") String itemId);

    @DAOMethod(sql = "update DisposalExpenseRecord set state=:state,applicationNo=:applicationNo,expenseIdSource=:expenseIdSource where  itemId =:itemId ")
    public abstract void updateByItemId( @DAOParam("state") Byte state,@DAOParam("applicationNo") String applicationNo,@DAOParam("expenseIdSource")String expenseIdSource, @DAOParam("itemId") String itemId);

    @DAOMethod(sql = "delete from DisposalExpenseRecord where itemId=:itemId")
    public abstract void deleteDisposalExpenseRecordByItemId(@DAOParam(value = "itemId") String itemId);

    @DAOMethod(sql = "delete from DisposalExpenseRecord where expenseId=:expenseId")
    public abstract void deleteDisposalExpenseRecord(@DAOParam(value = "expenseId") String expenseId);

    @DAOMethod(sql = "delete from DisposalExpenseRecord where applicationNo=:applicationNo")
    public abstract void deleteDisposalExpenseRecordByApplicationNo(@DAOParam(value = "applicationNo") String applicationNo);

    @DAOMethod(sql = "update DisposalExpenseRecord set state='0' where billCategoryCode=:type and consultId=:consultId and state='1'")
    public abstract void updateDisposalExpenseRecordState(@DAOParam("consultId")String consultId, @DAOParam("type") String type);

    @DAOMethod(sql = "update DisposalExpenseRecord set state=:state where applicationNo =:applicationNo ")
    public abstract void updateDisposalExpenseRecordCancelState(@DAOParam("applicationNo") String applicationNo, @DAOParam("state") Byte state);

    @DAOMethod(sql = "update DisposalExpenseRecord set pay_status=:pay_status where applicationNo =:applicationNo ")
    public abstract void updateDisposalExpenseRecordPayState(@DAOParam("applicationNo") String applicationNo,@DAOParam("pay_status") String pay_status);

    @DAOMethod(sql = "update DisposalExpenseRecord set pay_status=:pay_status ,state='2'," +
            " executive_department_id=:executive_department_id," +
            " executive_department_name=:executive_department_name  " +
            " where applicationNo =:applicationNo ")
    public abstract void updateDisposalExpenseRecordFeeState(@DAOParam("applicationNo") String applicationNo,
                                                             @DAOParam("pay_status") String pay_status,
                                                             @DAOParam("executive_department_id") String executive_department_id,
                                                             @DAOParam("executive_department_name") String executive_department_name);


    @DAOMethod(sql = " from DisposalExpenseRecord where expenseIdSource =:expenseIdSource and  organizationId=:organizationId and billCategoryCode=:billCategoryCode")
    public abstract DisposalExpenseRecord getDisposalExpenseRecordByExpenseIdSource(@DAOParam("expenseIdSource") String expenseIdSource, @DAOParam("organizationId") String organizationId, @DAOParam("billCategoryCode") String billCategoryCode);

    @DAOMethod(sql = " from DisposalExpenseRecord where applicationNo =:applicationNo and  organizationId=:organizationId and billCategoryCode=:billCategoryCode")
    public abstract DisposalExpenseRecord getDisposalExpenseRecordByApplicationNo(@DAOParam("applicationNo") String applicationNo, @DAOParam("organizationId") String organizationId, @DAOParam("billCategoryCode") String billCategoryCode);

    @DAOMethod(sql = " from DisposalExpenseRecord where expenseId in:expenseIdList ")
    public abstract List<DisposalExpenseRecord> queryDisposalExpenseRecordByExpenseIdList(@DAOParam("expenseIdList") List<String> expenseIdList);

    public DisposalExpenseRecord getDisExpenseRecordByApplicationNo(final String applicationNo, final String consultId) {
        HibernateStatelessResultAction<DisposalExpenseRecord> action = new AbstractHibernateStatelessResultAction<DisposalExpenseRecord>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer sql = new StringBuffer();
                sql.append(" select t.expense_id expenseId,t.organization_id organizationId,t.application_no applicationNo,t.expiration_date expirationDate ");
                sql.append("  from disposal_expense_record t where application_no=:applicationNo and consultId=:consultId limit 1 ");

                Query query = ss.createSQLQuery(sql.toString());
                query.setParameter("applicationNo", applicationNo);
                query.setParameter("consultId", consultId);
                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                DisposalExpenseRecord DisposalExpenseRecord = null;
                List<Map> list = query.list();
                if (list.size() > 0) {
                    for (Map map : list) {
                        DisposalExpenseRecord = ConversionUtils.convert(map, DisposalExpenseRecord.class);
                        break;
                    }
                }
                setResult(DisposalExpenseRecord);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

}
